#include "genomedatabaseinterface.h"

//init new database
GenomeDatabaseInterface::GenomeDatabaseInterface(QObject *parent) :
    QObject(parent)
{

}

//reconnect to existing database
GenomeDatabaseInterface::GenomeDatabaseInterface(QString dbName, QObject *parent) :
    QObject(parent)
{
    createDatabase( dbName );
}

//create new database
bool GenomeDatabaseInterface::createDatabase()
{
     db = QSqlDatabase::addDatabase("QSQLITE");
     db.setDatabaseName("genomeData.db");
        if (!db.open()) {
//            QMessageBox::critical(0, qApp->tr("Cannot open database"),
//                qApp->tr("Unable to establish a database connection.\n"
//                         "This example needs SQLite support. Please read "
//                         "the Qt SQL driver documentation for information how "
//                         "to build it.\n\n"
//                         "Click Cancel to exit."), QMessageBox::Cancel);
            qDebug() << "DATABASE NOT CREATED" << endl;
            return false;
        }
        //else... worked!
        qDebug() << "Database created" << endl;
}

bool GenomeDatabaseInterface::createDatabase(QString dbName )
{
     db = QSqlDatabase::addDatabase("QSQLITE");
     db.setDatabaseName(dbName);
        if (!db.open()) {
//            QMessageBox::critical(0, qApp->tr("Cannot open database"),
//                qApp->tr("Unable to establish a database connection.\n"
//                         "This example needs SQLite support. Please read "
//                         "the Qt SQL driver documentation for information how "
//                         "to build it.\n\n"
//                         "Click Cancel to exit."), QMessageBox::Cancel);
            qDebug() << "DATABASE NOT OPENNED" << endl;
            return false;
        }
        //else... worked!
        qDebug() << "Database openned" << endl;
}


void GenomeDatabaseInterface::addGenomeElementThenDeleteIt( GenomeElementDataPacket* dataPacket )
{
    addGenomeElement(dataPacket);
    //delete dataPacket;
}

void GenomeDatabaseInterface::addGenomeElement( GenomeElementDataPacket* dataPacket )
{
    //add data packet to db
}

void GenomeDatabaseInterface::batchAdd( QList< QPointer<GenomeElementDataPacket> > dataPackets )
{

}

void GenomeDatabaseInterface::batchAddThenDelete( QList< QPointer<GenomeElementDataPacket> > dataPackets  )
{

}

void GenomeDatabaseInterface::addContig( QPointer< ContigDataPacket> dataPacket  )
{

}

void GenomeDatabaseInterface::addContigThenDeleteIt( QPointer< ContigDataPacket> dataPacket  )
{

}

void GenomeDatabaseInterface::batchAdd( QList< QPointer< ContigDataPacket> > dataPackets )
{

}

void GenomeDatabaseInterface::batchAddThenDelete(QList< QPointer< ContigDataPacket> > dataPackets  )
{

}

void GenomeDatabaseInterface::getSortedByLength(QList<QPointer<ContigDataPacket> >&, QList<QPointer<GenomeElementDataPacket> >&, bool)
{

}

void GenomeDatabaseInterface::getContigsWithCluster(QString clusterId, QList<QPointer<ContigDataPacket> > &, QList<QPointer<GenomeElementDataPacket> > &)
{

}

void GenomeDatabaseInterface::getStringMatches(QString searchFragment, QList< QPointer<ContigDataPacket> >&, QList< QPointer<GenomeElementDataPacket> >& )
{

}


void GenomeDatabaseInterface::empty()
{
    qDebug() << "generic" << endl;
}

void GenomeDatabaseInterface::beginTransaction()
{
    db.transaction(); //begin transaction
}

void GenomeDatabaseInterface::commitTransaction()
{
    bool worked = db.commit();//commit transaction
    if( !worked )
    {
        qDebug() << "**** COMMIT FAILED ****" << endl;
        qDebug() << db.lastError().text() << endl;
    }
    else
    {
        qDebug() << "DB add succeeded: " << endl;
    }
}

//void GenomeDatabaseInterface::addContig( QPointer< ContigDataPacket> dataPacket  )
//{

//}

//void GenomeDatabaseInterface::addContigThenDeleteIt( QPointer< ContigDataPacket> dataPacket  )
//{
//    addContig(dataPacket);
//    //delete dataPacket;
//}
